package com.yanqu.road.dao.impl.activity.springunionbanquet;


import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.springunionbanquet.SpringUnionBanquetJoinData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class SpringUnionBanquetJoinDataDaoImpl extends TempDataObjectDao<SpringUnionBanquetJoinData> {


    public long getMaxJoinId(int activityId) {
        String sql = "select ifnull(max(joinId),0) as maxId from `t_c_spring_union_banquet_join` where `activityId`=?";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public static String getSpringUnionBanquetJoinDataSql = "select * from `t_c_spring_union_banquet_join` where `activityId`=?";
    public Map<Long, SpringUnionBanquetJoinData> getSpringUnionBanquetJoinData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, SpringUnionBanquetJoinData> result = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getSpringUnionBanquetJoinDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SpringUnionBanquetJoinData data = parse(rs);
                result.put(data.getJoinId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateSpringUnionBanquetJoinDataSql = "update `t_c_spring_union_banquet_join` set userId=?,banquetId=?,joinTime=?,reward=? where activityId=? and joinId=?";
    @Override
    protected boolean updateData(SpringUnionBanquetJoinData springUnionBanquetJoinData) {
        springUnionBanquetJoinData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetJoinData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetJoinData.getBanquetId()));
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetJoinData.getJoinTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, springUnionBanquetJoinData.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, springUnionBanquetJoinData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetJoinData.getJoinId()));
        if (getDb().executeNoneQuery(updateSpringUnionBanquetJoinDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSpringUnionBanquetJoinDataSql = "insert into `t_c_spring_union_banquet_join`(activityId,joinId,userId,banquetId,joinTime,reward) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(SpringUnionBanquetJoinData springUnionBanquetJoinData) {
        springUnionBanquetJoinData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, springUnionBanquetJoinData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetJoinData.getJoinId()));
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetJoinData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetJoinData.getBanquetId()));
        para.put(i++, new DbParameter(Types.BIGINT, springUnionBanquetJoinData.getJoinTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, springUnionBanquetJoinData.getReward()));
        if (getDb().executeNoneQuery(insertSpringUnionBanquetJoinDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SpringUnionBanquetJoinData parseData(ResultSet rs) throws SQLException {
        SpringUnionBanquetJoinData springUnionBanquetJoinData = new SpringUnionBanquetJoinData();
        springUnionBanquetJoinData.setActivityId(rs.getInt("activityId"));
        springUnionBanquetJoinData.setJoinId(rs.getLong("joinId"));
        springUnionBanquetJoinData.setUserId(rs.getLong("userId"));
        springUnionBanquetJoinData.setBanquetId(rs.getLong("banquetId"));
        springUnionBanquetJoinData.setJoinTime(rs.getLong("joinTime"));
        springUnionBanquetJoinData.setReward(rs.getString("reward"));

        return springUnionBanquetJoinData;
    }
}
